#!/usr/bin/env python
# -*- coding: UTF8 	-*-

from GladeConnect import GladeConnect
import gtk
import sys
from psycopg import connect

(RUT,
NOMBRE_PACIENTE,
GENERO) = range(3)

class wnReporte(GladeConnect):
    def __init__(self, padre=None):
        GladeConnect.__init__(self, "glade/reporte.glade")
        self.padre = padre
        self.dlgImpresora.hide()
        self.wnReporte.show()
        self.cnx = connect('dbname=pyscd')
        self.cnx.autocommit()
        self.cursor = self.cnx.cursor()
        self.edit = False
        self.crea_columnas()
        self.crea_modelo()


    def crea_columnas(self, source=None, event=None):        
        
        render = gtk.CellRendererText()
        column = gtk.TreeViewColumn('R.U.T. del Dentista.', render, text=RUT)
        self.treeReporte.append_column(column)
    
    	render = gtk.CellRendererText()
        column = gtk.TreeViewColumn('R.U.T. del Paciente', render, text=NOMBRE_PACIENTE)
        self.treeReporte.append_column(column)
        
    def crea_modelo(self):
        self.modelo = gtk.ListStore(str, str)
        
        completion = gtk.EntryCompletion()
        rut = self.entDentista
        self.liststore = gtk.ListStore(str)
        self.cursor.execute("""select rut_dentista from dentista order by nombres_dentista""")
        r = self.cursor.fetchall()
        ap = self.liststore.append
        map(ap, r)  
        completion.set_model(self.liststore)
        rut.set_completion(completion)
        completion.set_text_column(0)
        return



    def on_btnBuscar_clicked (self, btn=None): 
        rut = self.entDentista.get_text().upper()
        try:
            self.modelo.clear()
            sql = """SELECT * from dentista_paciente where rut_dentista = '%s'""" %(rut) 
            self.cursor.execute(sql)            
            records = self.cursor.fetchall()
            for record in records:
                self.modelo.append(record)
            self.treeReporte.set_model(self.modelo)
        except:
            print sys.exc_info()[1]
            self.stbReporte.push(0, "Error al obtener los datos.")


    def on_btnImprimir_clicked(self, btn=None, widget=None, data=None):
        self.dlgImpresora.show()

    def on_btnCancelar_clicked(self, btn=None):
        self.dlgImpresora.hide()
    

    def on_btnCerrar_clicked(self, btn=None, data=None):
        if self.padre is None:
            self.on_exit()
        else:
            self.wnCliente.hide()


if __name__ == "__main__":

    c = wnReporte()
    c.wnReporte.show()
    gtk.main()
